﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Controls.ServerControls;
using System.Web.UI.HtmlControls;
using QXBook.Utils;
using QXBook.Model;
using QXBook.Service;
using QXBook.Controls.HtmlControls;

namespace Web.SysManage.Product
{
    public partial class ProductCategoryList : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(28);

            if (!Page.IsPostBack)
            {
                this.TextBind();
            }

            btnAdd.Click += new EventHandler(btnAdd_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            lmc.EnableLinkPublish = false;
            lmc.EnableLinkOff = false;
            lmc.EnableLinkOn = false;
            lmc.EnableLinkShipments = false;
            lmc.EnableLinkResave = false;

            lmc.AddHandler += new EventHandler(lmc_AddHandler);

        }

        void lmc_AddHandler(object sender, EventArgs e)
        {
            List<CategoryInfo> list = GetRepeaterCategorys();

            CategoryInfo tmpcategory = new CategoryInfo();
            tmpcategory.CategoryName = "请输入分类名称";
            tmpcategory.ParentID = 0;
            list.Add(tmpcategory);

            this.rptList.DataSource = list;
            this.rptList.DataBind();
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            List<CategoryInfo> list = GetRepeaterCategorys();

            CategoryInfo tmpcategory = new CategoryInfo();
            tmpcategory.CategoryName = "请输入分类名称";
            tmpcategory.ParentID = 0;
            list.Add(tmpcategory);

            this.rptList.DataSource = list;
            this.rptList.DataBind();
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSave_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                HtmlInputHidden hidParentID = (HtmlInputHidden)item.FindControl("hidParentID");
                //HtmlInputHidden hidPart = (HtmlInputHidden)item.FindControl("hidPart");
                TextBoxLabel txtTitle = (TextBoxLabel)item.FindControl("txtTitle");

                //有修改过的行
                if (txtTitle.ControlDisplayType == DisplayType.TextBox)
                {
                    CategoryInfo category = new CategoryInfo();
                    category.CategoryName = txtTitle.Text;
                    category.ParentID = CommonUtils.ToInt(hidParentID.Value);
                    category.PartType = Part.RWDC;

                    //新增的行
                    if (string.IsNullOrEmpty(hidID.Value) || CommonUtils.ToInt(hidID.Value) == 0)
                    {
                        ServiceFactory.CreateInstance<CategoriesService>().Add(category);
                    }
                    else//更新的行
                    {
                        category.CategoryID = CommonUtils.ToInt(hidID.Value);
                        ServiceFactory.CreateInstance<CategoriesService>().Update(category);
                    }
                }
            }

            TextBind();
            base.ResponseErch("保存成功");
        }

        List<CategoryInfo> GetRepeaterCategorys()
        {
            List<CategoryInfo> list = new List<CategoryInfo>();
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                HtmlInputHidden hidParentID = (HtmlInputHidden)item.FindControl("hidParentID");
                //HtmlInputHidden hidPart = (HtmlInputHidden)item.FindControl("hidPart");
                Literal litStage = (Literal)item.FindControl("litStage");
                TextBoxLabel txtTitle = (TextBoxLabel)item.FindControl("txtTitle");

                CategoryInfo category = new CategoryInfo();
                category.CategoryID = CommonUtils.ToInt(hidID.Value);
                category.Stage = litStage.Text;
                category.CategoryName = txtTitle.Text;
                category.ParentID = CommonUtils.ToInt(hidParentID.Value);
                category.PartType = Part.RWDC;
                list.Add(category);
            }

            return list;
        }

        private void TextBind()
        {
            CategoriesService service = ServiceFactory.CreateInstance<CategoriesService>();
            service.CreateCategory(Part.RWDC, 0);

            this.rptList.DataSource = service.ListCategory;
            this.rptList.DataBind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            CategoryInfo category = (CategoryInfo)e.Item.DataItem;
            Span sId = (Span)e.Item.FindControl("sId");
            HtmlInputHidden hidID = (HtmlInputHidden)e.Item.FindControl("hidID");
            HtmlInputHidden hidParentID = (HtmlInputHidden)e.Item.FindControl("hidParentID");
            Literal litStage = (Literal)e.Item.FindControl("litStage");
            TextBoxLabel txtTitle = (TextBoxLabel)e.Item.FindControl("txtTitle");
            Span sAddDate = (Span)e.Item.FindControl("sAddDate");
            ImageButton imgbtnEdit = (ImageButton)e.Item.FindControl("imgbtnEdit");
            ImageButton imgbtnAdd = (ImageButton)e.Item.FindControl("imgbtnAdd");

            sId.Text = category.CategoryID.ToString();
            hidID.Value = category.CategoryID.ToString();
            hidParentID.Value = category.ParentID.ToString();
            litStage.Text = category.Stage;
            txtTitle.Text = category.CategoryName;

            if (category.CategoryID > 0)
            {
                txtTitle.ControlDisplayType = DisplayType.Label;
                imgbtnEdit.Visible = true;
                imgbtnAdd.Visible = true;
            }
            else
            {
                txtTitle.ControlDisplayType = DisplayType.TextBox;
                imgbtnEdit.Visible = false;
                imgbtnAdd.Visible = false;
            }
            sAddDate.Text = category.AddDate == CommonUtils.ToTime("0001/1/1 0:00:00") ? string.Empty : category.AddDate.ToString("yyyy-MM-dd");
        }

        protected void rptList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("Create", StringComparison.OrdinalIgnoreCase))
            {
                HtmlInputHidden hidID = (HtmlInputHidden)e.Item.FindControl("hidID");
                Literal litStage = (Literal)e.Item.FindControl("litStage");

                List<CategoryInfo> list = this.GetRepeaterCategorys();
                for (int i = 0, j = list.Count; i < j; i++)
                {
                    if (i == e.Item.ItemIndex)
                    {
                        CategoryInfo category = new CategoryInfo();
                        category.Stage = litStage.Text + "--";
                        category.CategoryName = "请输入分类名称";
                        category.ParentID = CommonUtils.ToInt(hidID.Value);
                        category.PartType = Part.RWDC;

                        list.Insert(i + 1, category);
                        break;
                    }
                }

                this.rptList.DataSource = list;
                this.rptList.DataBind();
                return;
            }

            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                TextBoxLabel txtTitle = (TextBoxLabel)e.Item.FindControl("txtTitle");

                txtTitle.ControlDisplayType = DisplayType.TextBox;
                return;
            }
        }
    }
}